# -*- coding: utf-8 -*-
from video import VideoDescriptor
from audio import AudioDescriptor
from sub import SubDescriptor

class FilmDescriptor():
    
    title    = None
    width    = None
    height   = None
    
    audio    = []
    video    = []
    sub      = []

    def __init__(self, name):
        self.title = name
        self.audio = []
        self.video = []
        self.sub = []
        
    def get_methods(self):
        return ['video', 'audio', 'sub']    
        
    def add_video(self, data):
        self.video.append(VideoDescriptor(data))
        
    def add_audio(self, data):
        self.audio.append(AudioDescriptor(data))
        
    def add_sub(self, data):
        self.sub.append(SubDescriptor(data))
        
        
    def getTitle(self):
        return self.title
    
    
    """
    retourne une chaine qui décrit la langue
    """
    def getLang(self):
        
        t = []
        ## langues audio
        for audio in self.audio:
            alang = audio.getLang()
            if(alang): t.append(self._(alang)) 

        if len(t) == 0:
            t.append(self._('?'))

        return t
    
    def getSubLang(self):
        t = []
        for sub in self.sub:
            t.append(self._(sub.getLang()))
        return t
    
    
    def getAudioCodec(self):
        for audio in self.audio:
            codec = audio.getCodec()
            if(codec): return codec
    
    def getAudioNbTracks(self):
        return str(len(self.audio))
    
    def getVideoCodec(self):
        for video in self.video:
            codec = video.getCodec()
            if(codec): return codec
            
    def getVideoQuality(self):
        for video in self.video:
            q = video.getQuality()
            if(q): return q
    
    def getVideoResolution(self):
        return "%sx%s" % (self.video[0].getWidth(), self.video[0].getHeight())
    
    def _(self, v):
        tr = {
                  'default' : 'FR',
                  'English' : 'EN',
                  'French' : 'FR',
              }
        try:
             v =  tr[v]
        except KeyError, err:
            print "* film::_ : '%s' is unknown, falling back to default" % v
            v = tr['default']
        return v